package com.wk.springboot.spring.aware;

import org.springframework.context.ApplicationContext;

/**
 * Author: Administrator
 * Date: 2020/11/25 : 21:09
 * Description: applicationContext工具类
 */
public final class ApplicationContextUtils {

    private static ApplicationContext applicationContext;

    static void setApplicationContext(ApplicationContext applicationContext) {
        ApplicationContextUtils.applicationContext = applicationContext;
    }

    /**
     * 根据class类获取bean对象
     * @param requiredType
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> requiredType){
        return applicationContext.getBean(requiredType);
    }

    /**
     * 根据名称和类型获取Bean
     * @param name
     * @param requiredType
     * @param <T>
     * @return
     */
    public static <T> T getBean(String name,Class<T> requiredType){
        return applicationContext.getBean(name,requiredType);
    }
}
